package it.koutifaris.travianmap.view.distances;

import it.koutifaris.travianmap.geometry.DistanceCalculator;
import it.koutifaris.travianmap.info.Troop;
import it.koutifaris.travianmap.info.Troops;
import it.koutifaris.travianmap.model.Village;
import it.koutifaris.travianmap.utils.VillageToPoint;

import java.util.List;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

public class DistancesViewInitializer {

	private final Village from;
	private final Village to;
	private Village currentFrom;
	private Village currentTo;
	private final Activity activity;

	public DistancesViewInitializer(Activity activity, Village from, Village to) {
		this.activity = activity;
		this.from = from;
		this.to = to;
		this.currentFrom = from;
		this.currentTo = to;
	}

	void initView(View view) {
		DistancesView distancesView = new DistancesViewImpl(view);

		List<Troop> troops = Troops.troops().getTroopsOfTribe(currentFrom.getOwner().getTribe());
		DistanceCalculator calculator =
				new DistanceCalculator(new VillageToPoint(currentFrom), new VillageToPoint(currentTo));

		distancesView.setDistance(calculator.getDistance());

		distancesView.getListView().setAdapter(new DistancesAdapter(getActivity(), distancesView, troops, calculator));

		initFromSpinner(distancesView);
		initToSpinner(distancesView);
	}

	private void initToSpinner(DistancesView distancesView) {
		VillagesSpinnerAdapter spinnerAdapter =
				new VillagesSpinnerAdapter(getActivity(), new VillageToPoint(currentFrom), to.getOwner().getVillages());
		getToSpinner(distancesView).setAdapter(spinnerAdapter);
		getToSpinner(distancesView).setSelection(spinnerAdapter.getPosition(currentTo));
		getToSpinner(distancesView).setOnItemSelectedListener(new OnToItemSelectedListenerImpl());
	}

	private Spinner getToSpinner(DistancesView distancesView) {
		return distancesView.getToSpinner();
	}

	private void initFromSpinner(DistancesView distancesView) {
		VillagesSpinnerAdapter spinnerAdapter =
				new VillagesSpinnerAdapter(getActivity(), new VillageToPoint(from), from.getOwner().getVillages());
		getFromSpinner(distancesView).setAdapter(spinnerAdapter);
		getFromSpinner(distancesView).setSelection(spinnerAdapter.getPosition(currentFrom));
		getFromSpinner(distancesView).setOnItemSelectedListener(new OnFromItemSelectedListenerImpl());
	}

	private Spinner getFromSpinner(DistancesView distancesView) {
		return distancesView.getFromSpinner();
	}

	private Activity getActivity() {
		return activity;
	}

	private class OnFromItemSelectedListenerImpl implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			Village selected = (Village) parent.getItemAtPosition(position);
			if (!currentFrom.equals(selected)) {
				currentFrom = selected;
				initView(parent.getRootView());
			}

		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}

	}

	private class OnToItemSelectedListenerImpl implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			Village selected = (Village) parent.getItemAtPosition(position);
			if (!currentTo.equals(selected)) {
				currentTo = selected;
				initView(parent.getRootView());
			}

		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}

	}

}
